#include "mainwindow.h"
#include "securitytools/securitytoolswidget.h"
#include "securitytools/securitytoolsmodel.h"

#include "securitytools/loginsafety/loginsafetywidget.h"
#include "securitytools/loginsafety/loginsafetymodel.h"

#include "securitytools/startup/startupwidget.h"
#include "securitytools/startup/autostartmodel.h"

#include "securitytools/trustedprotection/trustedprotectionwidget.h"

#include "securitytools/internetcontrol/netcontrolwidget.h"
#include "securitytools/datausage/datausagewidget.h"
#include "securitytools/internetcontrol/netctrlmodel.h"
#include "securitytools/datausage/datausagemodel.h"

#include "securitytools/usbstoragesecurity/usbstoragesecuritywidget.h"
#include "securitytools/usbstoragesecurity/usbstoragesecuritymodel.h"

#include <QStackedLayout>
#include <QMessageBox>
#include <QPushButton>
#include <QGridLayout>
#include <QSizePolicy>

#include "widgets/hfloatingdialog.h"

MainWindow::MainWindow(QWidget *parent)
    : QFrame(parent),
      mMainLayout(new QStackedLayout()),
      mModel(new SecurityToolsModel(this)),
      mHomePage(new SecurityToolsWidget(mModel)),
      mLoginSafetyWidget(nullptr),
      mLoginSafetyModel(nullptr),
      mStartupWidget(nullptr),
      mAutoStartModel(nullptr),
      mTrustedProtectionWidget(nullptr),
      mNetControlWidget(nullptr),
      mDataUsageWidget(nullptr),
      mNetCtrlModel(nullptr),
      mDataUsageModel(nullptr),
      mUSBStorageSecurityWidget(nullptr),
      mUSBStorageSecurityModel(nullptr)
{
    initUI();
    initconnect();
}

MainWindow::~MainWindow()
{

}

void MainWindow::initUI()
{
    QWidget* top = new QWidget(this);
    mToolBar = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addWidget(top);
    layout->addWidget(mToolBar, 0, Qt::AlignLeft);
    mToolBar->setFixedHeight(30);
    mToolBar->setContentsMargins(0, 0, 0, 0);
    top->setContentsMargins(0, 0, 0, 0);
    top->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    mToolBar->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);


    mMainLayout->setMargin(0);
    top->setLayout(mMainLayout);
    mMainLayout->addWidget(mHomePage);

    mBack = new QPushButton(this);
    mBack->setFixedSize(120, 30);
    mBack->setText(tr("Back"));
    QHBoxLayout* bottomLayout = new QHBoxLayout(mToolBar);
    mToolBar->setLayout(bottomLayout);
    bottomLayout->setMargin(0);
    bottomLayout->addWidget(mBack, 0, Qt::AlignLeft);
    connect(mBack, SIGNAL(clicked()), this, SLOT(showHomePage()));
    mToolBar->hide();
}

void MainWindow::initconnect()
{
    connect(mHomePage, SIGNAL(notifyShowLoginSafetyWidget()), this, SLOT(showLoginSafetyWidget()));
    connect(mHomePage, SIGNAL(notifyShowStartupWidget()), this, SLOT(showStartupWidget()));
    connect(mHomePage, SIGNAL(notifyShowTrustedProtectionWidget()), this, SLOT(showTrustedProtectionWidget()));
    connect(mHomePage, SIGNAL(notifyShowNetControlWidget()), this, SLOT(showNetControlWidget()));
    connect(mHomePage, SIGNAL(notifyShowDataUsageWidget()), this, SLOT(showDataUsageWidget()));
    connect(mHomePage, SIGNAL(notifyShowUsbControlWidget()), this, SLOT(showUsbControlWidget()));
}

void MainWindow::showHomePage()
{
    mMainLayout->setCurrentWidget(mHomePage);
    mToolBar->hide();
}

void MainWindow::showLoginSafetyWidget()
{
    if (!mLoginSafetyModel) {
        mLoginSafetyModel = new LoginSafetyModel(this);
    }
    if (!mLoginSafetyWidget) {
        mLoginSafetyWidget = new LoginSafetyWidget(mLoginSafetyModel, this);
        mMainLayout->addWidget(mLoginSafetyWidget);
    }
    mMainLayout->setCurrentWidget(mLoginSafetyWidget);
    mToolBar->show();
}

void MainWindow::showStartupWidget()
{
    if (!mAutoStartModel) {
        mAutoStartModel = new AutoStartModel(this);
    }
    if (!mStartupWidget) {
        mStartupWidget = new StartupWidget(mAutoStartModel, this);
        mMainLayout->addWidget(mStartupWidget);
    }
    mMainLayout->setCurrentWidget(mStartupWidget);
    mToolBar->show();
}

void MainWindow::showTrustedProtectionWidget()
{
    if (!mTrustedProtectionWidget) {
        mTrustedProtectionWidget = new TrustedProtectionWidget(this);
        mMainLayout->addWidget(mTrustedProtectionWidget);
    }
    mMainLayout->setCurrentWidget(mTrustedProtectionWidget);
    mToolBar->show();
}

void MainWindow::showNetControlWidget()
{
    if (!mNetCtrlModel) {
        mNetCtrlModel = new NetCtrlModel(this);
    }
    if (!mNetControlWidget) {
        mNetControlWidget = new NetControlWidget(mNetCtrlModel, "");
        mMainLayout->addWidget(mNetControlWidget);
    }
    mMainLayout->setCurrentWidget(mNetControlWidget);
    mToolBar->show();
}

void MainWindow::showDataUsageWidget()
{
    if (!mDataUsageModel) {
        mDataUsageModel = new DataUsageModel(this);
    }
    if (!mDataUsageWidget) {
        mDataUsageWidget = new DataUsageWidget(mDataUsageModel);
        mMainLayout->addWidget(mDataUsageWidget);
    }
    mMainLayout->setCurrentWidget(mDataUsageWidget);
    mToolBar->show();
}

void MainWindow::showUsbControlWidget()
{
    if (!mUSBStorageSecurityModel) {
        mUSBStorageSecurityModel = new USBStorageSecurityModel(this);
    }
    // 判断是否已加域
    if (mUSBStorageSecurityModel->isInUDCPDomain()) {
        DDialog dlg;
        dlg.setAccessibleName("isInUDCPDialog");
        dlg.setModal(true);
        dlg.setOnButtonClickedClose(true);
        // 设置左上角图标
        dlg.setIcon(QIcon::fromTheme(DIALOG_TIP_YELLOW));
        dlg.setMessage(tr("The USB connection policy has been fully managed by your domain administrator, please contact the admin for help"));
        dlg.setWordWrapMessage(true);
        dlg.addButton(tr("OK", "button"), true);
        dlg.exec();

        return;
    }
    // 需要判断usb安全页是否已经创建，避免重复创建，造成usb安全页面响应两次
    if (!mUSBStorageSecurityWidget) {
        mUSBStorageSecurityWidget = new USBStorageSecurityWidget(mUSBStorageSecurityModel, this);
        mMainLayout->addWidget(mUSBStorageSecurityWidget);
    }
    mMainLayout->setCurrentWidget(mUSBStorageSecurityWidget);
    mToolBar->show();
}
